package com.thingsgrid.device.monitor.controller;
import com.thingsgrid.device.entity.PerformanceMetrics;
import com.thingsgrid.device.monitor.service.IMertricsService;
import com.thingsgrid.device.monitor.utils.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 *指标管理
 */
@AllArgsConstructor
@RestController
@RequestMapping("/metrics_manage")
@Api(value = "指标管理", tags = "指标管理")
public class MetricsManageController {

	private IMertricsService mertricsService;




	/**
	 *
	 *查询指标名称
	 * @author 陈学栋
	 * @date 2019/9/5 9:31
	 */
	@GetMapping("/findmertrics_name")
	@ApiOperation(value = "查询指标名称", notes = "查询指标名称", position = 1)
	public R<List<PerformanceMetrics>> classify(){
			List<PerformanceMetrics> MetricsList =  mertricsService.findMertricsList();
		return R.data(MetricsList);
	}
	/**
	 *
	 *条件查询指标管的列表
	 * @author 陈学栋
	 * @date 2019/8/23 13:59
	 */
	@PostMapping("/findmertrics_list")
	@ApiOperation(value = "根据指标名称和类型查询列表", notes = "根据指标名称和类型查询列表", position = 2)
	public R<Map<String, Object>> findListByNameAndType(@RequestParam String metricsName, @RequestParam String metricsType,@RequestParam Integer current,@RequestParam Integer size){
		Map<String, Object> map =  mertricsService.findListByNameAndType(metricsName,metricsType,current,size);
		return R.data(map);
	}

	/**
	 *
	 *添加指标管理
	 * @author 陈学栋
	 * @date 2019/8/26 9:06
	 */
	@PostMapping("/add_mertrics")
	@ApiOperation(value = "添加指标管理", notes = "添加指标管理", position = 3)
	public R addMertrics(@RequestBody PerformanceMetrics performanceMetrics,@RequestParam Integer userId){
		Boolean flag = mertricsService.addMertrics(performanceMetrics,userId);
		return R.status(flag);
	}



	/**
	 *
	 *删除指标管理
	 * @author 陈学栋
	 * @date 2019/8/26 9:06
	 */
	@PostMapping("/delete_mertrics")
	@ApiOperation(value = "删除指标管理", notes = "删除指标管理", position = 4)
	public R deleteMertrics(String metricsIds){
		return R.status(mertricsService.deleteMertrics(metricsIds));
	}

	/**
	 *
	 *导出报表
	 * @author 陈学栋
	 * @date 2019/8/26 15:51
	 */
	@GetMapping("exportExcel")
	@ApiOperation(value = "导出报表", notes = "导出报表", position = 5)
	public R writeExcel(HttpServletResponse response) throws Exception {
		List<PerformanceMetrics> mertricsList = mertricsService.findMertricsList();
		if(mertricsList != null){
			String[] excelHeaders ={"指标编号","指标名称","指标编码","指标类型","指标描述","创建时间","创建人","备注"};
			String[] fields ={"metricsNumber","metricsName","metricsCode","metricsType","describe","createTime","createAuthor","metricsNote"};
			ExcelUtils.publicExport(mertricsList, PerformanceMetrics.class, "TABLE", excelHeaders, fields, response);
			return R.status(true);
		}else{
			return R.status(false);
		}
	}
}
